﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Json;

namespace JXT.PrimaryKey.Batman.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// 将字符串转化为数字(转换失败返回0)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>数字</returns>
        public static int ToInt32(this string s)
        {
            int result;
            if (!Int32.TryParse(s, out result))
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 将字符串转化为数字(转换失败返回0)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>数字</returns>
        public static long ToInt64(this string s)
        {
            long result;
            if (!Int64.TryParse(s, out result))
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 将字符串转化为数字(转换失败返回0)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>数字</returns>
        public static double ToDouble(this string s)
        {
            double result;
            if (!Double.TryParse(s, out result))
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 将字符串转化为数字(转换失败返回0)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>数字</returns>
        public static float ToFloat(this string s)
        {
            float result;
            if (!Single.TryParse(s, out result))
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 将字符串转化为数字(转换失败返回0)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>数字</returns>
        public static byte ToByte(this string s)
        {
            byte result;
            if (!Byte.TryParse(s, out result))
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 将字符串转化为GUID(转换失败返回Guid.Empty)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>GUID</returns>
        public static Guid ToGuid(this string s)
        {
            Guid result;
            if (!Guid.TryParse(s, out result))
            {
                result = Guid.Empty;
            }
            return result;
        }

        /// <summary>
        /// 将GUID转换为32字节小写字符串
        /// </summary>
        /// <param name="guid">guid值</param>
        /// <returns>转换过的字符串</returns>
        public static string ToGuidString(this Guid guid)
        {
            return guid.ToString("N");
        }

        /// <summary>
        /// 将字符串转化为bool(转换失败返回false)
        /// </summary>
        /// <param name="s">字符串值</param>
        /// <returns>bool>
        public static bool ToBoolean(this string s)
        {
            bool result;
            if (!Boolean.TryParse(s, out result))
            {
                try
                {
                    result = Convert.ToBoolean(s);
                }
                catch
                {
                    result = false;
                }
            }
            return result;
        }

        private static readonly string _isNumberPattern = "^\\d+$";
        /// <summary>
        /// 检测字符串是否为数字
        /// </summary>
        /// <param name="s">字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsNumber(this string s)
        {
            if (String.IsNullOrEmpty(s))
                return false;
            return Regex.IsMatch(s, _isNumberPattern);
        }

        /// <summary>
        /// 判断字符串是否为英文字符
        /// </summary>
        /// <param name="s">字符串</param>
        /// <returns>判定结果</returns>
        public static bool IsLetter(this string s)
        {
            if (String.IsNullOrEmpty(s))
                return false;
            return Regex.IsMatch(s, "^[a-z|A-Z]+$");
        }

        /// <summary>
        /// 将对象转化为Json字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="instanse">对象本身</param>
        /// <returns>JSON字符串</returns>
        public static string ToJsonString<T>(this T instanse)
        {
            try
            {
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T));    
                using (MemoryStream ms = new MemoryStream())
                {
                    js.WriteObject(ms, instanse);
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    StreamReader sr = new StreamReader(ms);
                    return sr.ReadToEnd();
                }
            }
            catch
            {
                return String.Empty;
            }
        }

        /// <summary>
        /// 将字符串转化为JSON对象，如果转换失败，返回default(T)
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="s">字符串</param>
        /// <returns>转换值</returns>
        public static T ToJsonObject<T>(this string s)
        {
            try
            {
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream())
                {
                    StreamWriter sw = new StreamWriter(ms);
                    sw.Write(s);
                    sw.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    return (T)js.ReadObject(ms);
                }
            }
            catch
            {
                return default(T);
            }
        }
    }
}
